<template>
  <div class="common-layout">
    <el-container>
      <el-header>
        <el-form :inline="true" :model="searchForm" class="blackWhiteSearch">
          <el-form-item label="场景名:">
            <el-input
              v-model="searchForm.sceneName"
              placeholder="场景名"
              style="width: 150px"
            ></el-input>
          </el-form-item>
          <el-form-item label="维度英文名:">
            <el-input
              v-model="searchForm.engName"
              placeholder="维度名"
              style="width: 150px"
            ></el-input>
          </el-form-item>
          <el-form-item label="名单类型:">
            <el-select
              v-model="searchForm.type"
              clearable
              placeholder="名单类型"
              style="width: 150px"
            >
              <el-option label="黑名单" value="1"></el-option>
              <el-option label="白名单" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSearch(searchForm)"
              >搜索</el-button
            >
            <el-button type="primary" @click="onAdd">新增</el-button>
          </el-form-item>
        </el-form>
      </el-header>
      <el-main>
        <el-table
          :data="tableData"
          style="width: 100%"
          height="500"
          border
          stripe
          highlight-current-row
        >
          <el-table-column prop="id" label="名单标识" width="80" />
          <el-table-column
            prop="blackWhiteTypeName"
            label="名单类型"
            width="100"
          />
          <el-table-column prop="sceneId" label="场景ID" width="100" />
          <el-table-column prop="sceneName" label="场景名" width="100" />
          <el-table-column prop="dimId" label="维度ID" width="100" />
          <el-table-column prop="dimName" label="维度名" width="100" />
          <el-table-column prop="dimEngName" label="维度英文名" width="100" />
          <el-table-column prop="dimVal" label="维度值" width="120" />
          <el-table-column prop="createTime" label="创建时间" width="180" />
          <el-table-column prop="updateTime" label="更新时间" width="180" />
          <el-table-column fixed="right" label="操作" width="120">
            <template #default="scope">
              <el-button
                type="text"
                size="small"
                @click.prevent="deleteRow(scope)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
    </el-container>
  </div>

  <el-dialog v-model="dialogFormVisible" title="新增名单">
    <el-form :model="addForm" class="addblackWhiteInfo">
      <el-form-item label="名单类型:" :label-width="formLabelWidth">
        <el-radio-group v-model="addForm.type">
          <el-radio :label="1">黑名单</el-radio>
          <el-radio :label="2">白名单</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="场景名:" :label-width="formLabelWidth">
        <el-select v-model="addForm.sceneName" placeholder="请关联一个场景" clearable @change="selectDim">
          <el-option
            v-for="item in sceneInfos"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="维度名:" :label-width="formLabelWidth">
        <el-select v-model="addForm.dimId" placeholder="请关联一个维度" clearable>
          <el-option
            v-for="item in dimensionInfos"
            :key="item.id"
            :value="item.id"
            :label="item.dimChineseName"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="维度值:" :label-width="formLabelWidth">
        <el-input v-model="addForm.dimVal"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="saveBlackWhite(addForm)">保存</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import { reactive, ref } from "vue";
import { UploadFilled } from "@element-plus/icons-vue";
import { BlackWhiteManage, SceneManage } from "@/api/index";
import { ElMessage } from "element-plus";

export default {
  components: {
    UploadFilled,
  },
  setup() {
    const radio = ref();

    const sceneInfos = ref()
    const dimensionInfos = ref()

    let dialogFormVisible = ref(false);
    const formLabelWidth = "140px";

    const addForm = reactive({
      name: "",
      type: "",
      jarFileInfo: File,
    });

    const searchForm = reactive({
      sceneName: "",
      engName: "",
      type: "",
    });

    const tableData = ref();

    const onSearch = () => {
      let searchCondition = {
        sceneName: "",
        engName: "",
        type: "",
      };

      searchCondition.sceneName = searchForm.sceneName;
      searchCondition.engName = searchForm.engName;
      searchCondition.type = searchForm.type;

      BlackWhiteManage.getBlackWhiteInfo(searchCondition).then(
        (response: any) => {
          if (response != undefined && response.status == 200) {
            const data: any = response.data;
            if (data.code != 0) {
              ElMessage.error(data.msg);
            } else {
              tableData.value = data.data;
            }
          } else {
            ElMessage.error("名单查询失败");
          }
        }
      );
    };

    const selectDim = (sceneId:any) => {
      // 初始化场景信息
      SceneManage.getSceneDimInfo({sceneId :sceneId}).then(
        (response: any) => {
          if (response != undefined && response.status == 200) {
            const data: any = response.data;
            if (data.code != 0) {
              ElMessage.error(data.msg);
            } else {
              dimensionInfos.value = data.data;
            }
          } else {
            ElMessage.error("获取场景维度信息失败");
          }
        }
      );
    }

    const onAdd = () => {
      dialogFormVisible.value = true;
      // 初始化场景信息
      SceneManage.getSceneInfo({}).then(
        (response: any) => {
          if (response != undefined && response.status == 200) {
            const data: any = response.data;
            if (data.code != 0) {
              ElMessage.error(data.msg);
            } else {
              sceneInfos.value = data.data;
            }
          } else {
            ElMessage.error("获取场景信息失败");
          }
        }
      );
    };

    const deleteRow = (scope: any) => {
      // 删除行
      BlackWhiteManage.delBlackWhiteInfo({ blackeWhiteId: scope.row.id }).then(
        (response: any) => {
          if (response != undefined && response.status == 200) {
            const data: any = response.data;
            if (data.code != 0) {
              ElMessage.error(data.msg);
            } else {
              tableData.value.splice(scope.$index, 1);
            }
          } else {
            ElMessage.error("名单删除失败");
          }
        }
      );
    };

    const saveBlackWhite = (addForm: any) => {
      BlackWhiteManage.addBlackWhiteInfo(addForm).then((response: any) => {
        if (response != undefined && response.status == 200) {
          const data: any = response.data;
          if (data.code != 0) {
            ElMessage.error(data.msg);
          } else {
            ElMessage.info("名单添加成功");
            dialogFormVisible.value = false;
            onSearch();
          }
        } else {
          ElMessage.error("名单添加失败");
        }
      });
    };

    const jarFile = ref();
    const onUploadChange = (file: any, fileList: any) => {
      // jarFile.value = file;
      addForm.jarFileInfo = file;
    };

    // 初始加载执行
    onSearch();
    return {
      searchForm,
      addForm,
      formLabelWidth,
      tableData,
      dialogFormVisible,
      sceneInfos,
      dimensionInfos,
      deleteRow,
      onAdd,
      onSearch,
      saveBlackWhite,
      onUploadChange,
      selectDim
    };
  },
};
</script>

<style lang="postcss" scoped>
.addblackWhiteInfo {
  position: inherit;
}

.el-button--text {
  margin-right: 15px;
}
.el-select {
  width: 300px;
}
.el-input {
  width: 300px;
}
.dialog-footer button:first-child {
  margin-right: 10px;
}

.blackWhiteSearch {
  position: relative;
  left: 0px;
}

.common-layout .el-header {
  background-color: #b3c0d1;
  color: var(--el-text-color-primary);
  text-align: center;
  line-height: 60px;
}

.common-layout .el-main {
  background-color: #e9eef3;
  color: var(--el-text-color-primary);
  text-align: center;
  /* line-height: 5px; */
}

.common-layout > .el-container {
  /* margin-bottom: 40px; */
}
</style>
